package com.kungfuhacking.wristbandpro.location.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.Basebean;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.location.bean.AffectionBean;
import com.kungfuhacking.wristbandpro.location.bean.WhiteListEntity;
import com.kungfuhacking.wristbandpro.location.view.IAffectionView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 亲情号码
 * Created by Aaron on 17/4/26.
 */

public class IAffectionPresenter implements BasePresenter {

    private IAffectionView iAffectionView;
    private String TAG = getClass().getSimpleName();
    private List<WhiteListEntity> lastAffectionList = new ArrayList<>();

    public IAffectionPresenter(IAffectionView iAffectionView) {
        this.iAffectionView = iAffectionView;
    }

    /**
     * 获取亲情号码列表
     */
    public void getAffectionList() {

        JSONObject j = new JSONObject();
        j.put("token", iAffectionView.getToken());
        j.put("id", iAffectionView.getSp().getString(Constant.CURRENTOLDID, ""));

        RetrofitHttpUtil.getInstance().affectionList(new Observer<AffectionBean>() {
            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iAffectionView.showLoading("获取中...");
            }

            @Override
            public void onNext(AffectionBean affectionBean) {
                iAffectionView.hideLoading();
                //缓存修改之前的list
                lastAffectionList.addAll(affectionBean.getWhiteList());

                if (affectionBean.getCode() == 0)
                    setAffectionInfo(affectionBean.getWhiteList());
                else
                    iAffectionView.showErr(affectionBean.getCode_msg());

            }

            @Override
            public void onError(Throwable e) {
                iAffectionView.hideLoading();
                iAffectionView.showErr("网络错误 : " + e.getLocalizedMessage());
            }

            @Override
            public void onComplete() {

            }
        }, new RequestBean().setData(j.toString()));

    }

    public void updateAffectionList(List<WhiteListEntity> affectionBeanList) {

        if (affectionBeanList.size() > 10) {
            iAffectionView.showErr("亲情号码不能多于10个");
            return;
        }

        AffectionBean affection = new AffectionBean();
        affection.setToken(iAffectionView.getToken());
        affection.setWhiteList(affectionBeanList);
        affection.setId(iAffectionView.getSp().getString(Constant.CURRENTOLDID, ""));
//        affection.setId("11111");

        RetrofitHttpUtil.getInstance().updateAffectionList(new BaseNoTObserver<Basebean>() {
            @Override
            public void onHandleSuccess(Basebean basebean) {
                iAffectionView.hideLoading();
            }

            @Override
            public void onHandleError(String message) {
                iAffectionView.showErr(message);
                iAffectionView.hideLoading();
                //更新出错,
                LogUtil.d(TAG, "lastAffectionBean的个数:" + lastAffectionList.size());
                setAffectionInfo(lastAffectionList);
            }

            @Override
            public void onReLogin(String message) {
                iAffectionView.showErr(message);
                iAffectionView.hideLoading();
                iAffectionView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {
                compositeDisposable.add(d);
                iAffectionView.showLoading("更新中...");
            }
        }, new RequestBean().setData(JSONObject.toJSONString(affection)));

    }

    private void setAffectionInfo(List<WhiteListEntity> whiteList) {
        iAffectionView.notifyInfo(whiteList);
    }

    /**
     * 检查输入的亲情号码是否正确
     */
    public boolean checkAffectionInput(List<WhiteListEntity> affectionBeanList) {

        if (TextUtils.isEmpty(iAffectionView.getAffectionNum()) ||
                iAffectionView.getAffectionNum().length() != 11) {
            iAffectionView.showErr("请输入正确的手机号码");
            return false;
        }

        if (TextUtils.isEmpty(iAffectionView.getAffectionName())) {
            iAffectionView.showErr("请输入昵称");
            return false;
        }

        if (affectionBeanList == null)
            affectionBeanList = new ArrayList<>();

        WhiteListEntity whiteListEntity = new WhiteListEntity();
        whiteListEntity.setName(iAffectionView.getAffectionName());
        whiteListEntity.setPhoneNum(iAffectionView.getAffectionNum());
        affectionBeanList.add(whiteListEntity);

        updateAffectionList(affectionBeanList);

        return true;
    }

    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
